<?php


class OrderAction extends SecurityAction{
	function _initialize(){
        $this->assign("menuFirst",session('menuFirst')); 
        $this->assign("menuSecond",session('menuSecond')); 
    }
		
	function olist(){
		
	/*	if ($_GET['opt'] == 'add'){  //新增跳转
			$this->display('Order/detail');
			return;
		}

	    if ($_GET["opt"] == 'edit'){   //编辑
	        $ob = M("yq_shop");
	        $info = $ob->where(array("tid"=>$_GET["tid"]))->find();
	        $this->assign("modify", '1');
	        $this->assign("info", $info);

	        $this->display('Order/detail');
	        return;
	    }*/
		
		$year = "";
		$month = "";
		$shopid = $_GET["shopid"];
		
		if ($_GET["year"]){
			$year = $_GET["year"];
		}else{
			$year = date("Y", time());
		}
		if ($_GET["month"]){
			$month = $_GET["month"];
		}else{
			$month = date("m", time());
		}
		

		$ob = M("yq_order");
		
		if($_GET['account']){
		    $sql[] = array('account'=>array('like',"%{$_GET['account']}%"));
		}
		
		if($_GET['year'] || $_GET['month']){
			$sql[] = array('created_at'=>array('like',"{$year}-{$month}%"));
		}
		
		if($shopid){
			$sql[] = array('yq_order.shopid'=>$shopid);
		}
		
		
		$cpp = 20; //分页数
        import('ORG.Util.Page');// 导入分页类
        $count      = $ob->join("yq_custom c on yq_order.customid = c.tid")->join(" yq_shop s on s.tid = yq_order.shopid")->field("yq_order.tid as orderid,yq_order.shopid,yq_order.customid,yq_order.price,yq_order.created_at,c.realname,s.shopname ")->where($sql)->count();// 查询满足要求的总记录数
        $Page       = new Page($count,$cpp);// 实例化分页类 传入总记录数和每页显示的记录数
        $show       = $Page->show();// 分页显示输出
        
		$list = $ob->join("yq_custom c on yq_order.customid = c.tid")
            		->join(" yq_shop s on s.tid = yq_order.shopid")
            		->join(" yq_chat ch on ch.orderid = yq_order.tid")
            		->join(" yq_member m on yq_order.memberid = m.tid ")
		                  ->field("yq_order.tid as orderid,yq_order.shopid,
		                      yq_order.customid,yq_order.price,yq_order.created_at,
		                      c.realname,s.shopname,yq_order.* 
		                      ,ch.status as chatstatus,m.realname as custName ")->where($sql)->order("tid desc")
		                  ->limit($Page->firstRow.','.$Page->listRows)->select();

		$this->assign("list", $list);
		$this->assign('show',$show);// 赋值分页输出

		$shopOb = M("yq_shop");
		$shoplist = $shopOb->select();
		$this->assign("shoplist", $shoplist);
		
		$this->display("Order/olist");
	}
	

	function detail(){
		$ob = M("yq_order");
		if ($_GET["opt"] == 'edit'){ 
			$info = $ob->join("yq_custom c on yq_order.customid = c.tid")
					   ->join(" yq_shop s on s.tid = yq_order.shopid")
					   ->join(" yq_member m on yq_order.memberid = m.tid ")
					   ->field("yq_order.tid as orderid,yq_order.shopid,yq_order.customid,yq_order.price,yq_order.discount_price,yq_order.cate,yq_order.process,yq_order.perm_type,yq_order.salon_type,yq_order.hot_time,
				yq_order.stay_time,yq_order.degree,yq_order.difftxt,yq_order.suggest,
				yq_order.is_forbit,yq_order.created_at,m.realname as custName,m.tid,
				c.realname,s.shopname ")->where(array("yq_order.tid"=>$_GET['tid']))->find();

			$detail = M("yq_ordergoods");
			$list = $detail->where(array("order_id"=>$_GET['tid']))->select();

			$this->assign("info", $info);
			$this->assign("list", $list);
			$this->display('/Order/detail');
			return;
	  	}

	  	if ($_GET["opt"] == 'del'){ 
	  		$roleid = session("info.roleid");
	  		if ($roleid == '1'){ //总部才可删除订单
		  		if ($_GET['tid']){
		  			$ob->where(array("tid"=>$_GET['tid']))->delete();
		  			$this->redirect("Order/olist");
		  		}
		  	}
		  	$this->error('您没有权限删除');
	  		return;
	  	}
	    
	}


	function chatlist(){
		if ($_GET['opt'] == 'ct'){
			$ob = M("yq_order");
			$info = $ob->join("yq_custom c on yq_order.customid = c.tid")->join(" yq_shop s on s.tid = yq_order.shopid")->join("yq_chat d on d.orderid = yq_order.tid")->field("yq_order.tid as orderid,yq_order.shopid,yq_order.customid,yq_order.price,c.realname,s.shopname,d.tid as chatid,d.content,d.status ")->where(array("yq_order.tid"=>$_GET['tid']))->find();

			$this->assign("info", $info);
			$this->display('/Order/chat');
			return;
		}

		if ($_POST){
			$_POST['adminid'] = session('info.tid');
	        $_POST['add_time'] = time();
	        $ob = M("yq_chat");
	        if ($_POST['tid'] != ''){
	        	$ob->where(array('tid'=>$_POST['tid']))->save($_POST);
	        }else{
	        	$ob->add($_POST);
	        }
	        $this->redirect("Order/olist");
	        return;
		}

	}

}

?>